const three = this.pageObj.getApiWidgetByAliasName('三维组件');
console.log('-------------- org init -----------------')

/**
 * {
    "communicationAccountType": "1680865008737390594",
    "levelName": "一级",
    "equipmentId": "ce369554-53b1-413b-b9ab-2e74e159d7db",
    "typeCode": "dispatchDesk",
    "personName": "李振",
    "xCoordinate": "106.665561821",
    "levelCode": "one",
    "yCoordinate": "29.487195281",
    "personImg": "[{\"type\":\"img\",\"name\":\"calmfox.png\",\"status\":\"done\",\"uid\":\"vc-upload-1702281729154-8\",\"url\":\"base/img/2575ba0d20934faf8829197bce00f18a.png\"}]",
    "communicationName": "值班李振",
    "communicationAccountLevel": "1680864909097504770",
    "zCoordinate": "-23.509262020",
    "personId": "1696049460969058305",
    "account": "10001",
    "equipmentTypeCode": "wjscj"
}
 * @param item
 */
function setTitle ({ typeCode, communicationName, personName, username, userId } = {}) {
  let title = '';

  if (typeCode === 'dispatchDesk') title += '调度台';

  title += (communicationName || username || personName);

  if (userId) title += `(${userId})`;

  return title
}

function breakDataSource(item) {
  if(item.pointId || item.communicationName || item.personName) {
    item.id = item.personId;
    item.userId = item.account || item.communicationAccount;
    item.username = item.communicationName || item.personName;
    item.title = setTitle(item);
    item.treeId = item.userId;
    item.disabled = true
  } else if(item.orgId || item.orgName) {
    item.title = item.orgName;
    item.id = item.orgId;
    item.treeId = item.orgId;
  } else if (item.deptId || item.deptName) {
    item.title = item.deptName;
    item.id = item.deptId;
    item.treeId = item.deptId;
  } else if (item.distributionGroupId || item.distributionGroupName) {
    item.title = item.distributionGroupName;
    item.id = item.distributionGroupId;
    item.treeId = item.distributionGroupId;
  } else if (item.equipmentTypeName || item.equipmentTypeCode) {
    item.title = item.equipmentTypeName;
    item.id = item.equipmentTypeCode;
    item.treeId = item.equipmentTypeCode;
  }

  if (item.orgList) item.children = item.orgList.map(breakDataSource);
  if (item.groupList) item.children = item.groupList.map(breakDataSource);
  if (item.personList) item.children = item.personList.map(breakDataSource);
  if (item.accountData) item.children = item.accountData.map(breakDataSource);
  if (item.equipmentType) item.children = item.equipmentType.map(breakDataSource);

  return item
}

fetch(`/Communication/pubCommunicationAccount/getOrgListByName`)
  .then(res => res.json())
  .then(res => {
    const { result, resultCode } = res;
    if (resultCode === '000000') {
      let dataSource = result.map(breakDataSource);
      const artemis = three?.widgetObj.artemis;

      if (artemis) {
        dataSource = artemis.resetUsersStatus(dataSource);
      }

      this.widgetObj._option.dataSource = dataSource;
      this.widgetObj.render();
      console.log(res, dataSource)
    }
  }).catch(error => console.error(error));
